package graphicswithsomebackup;

//************************' SOURCES *****************************************
// (1) code for KeyListener: http://www.youtube.com/watch?v=p9Y-NBg8eto
// (2) code taken from http://www.java2s.com/Code/Java/Swing-JFC/Panelwithbackgroundimage.htm
// (3) code taken from http://www.javaprogrammingforums.com/java-code-snippets-tutorials/390-how-get-mouse-position.html --- mouseInfo
//***************************************************************************
import Enemy.SpaceBear;
import Menu.Menu;
import Menu.Button;
import Menu.HighScoreHandler;
import Player.Player;


import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.MouseInfo;
import java.awt.Graphics;
import javax.swing.*;


public class GamePanel extends JPanel implements ActionListener, KeyListener, MouseListener  {

    // --------- Primitive types --------------    
    private boolean buttons = true;
    private boolean fetchingValues = true;
    private boolean gameIsOn = false;
    private boolean menuIsOn = true;
    private boolean showHighScore = false;
    private boolean mousePressed;
    private boolean writeName = true;

    private int panelWidth;
    private int panelHeight;
    private int showCount = 0;
    int mouseX;
    int mouseY;
    //.........................................

    // --------- Reference types --------------
    BackGround bg;
    HighScoreHandler highscoreHandler;
    Menu menu;
    MouseLocator mouseLocator;
    Timer timer;
    Player mPlayer;
    SpaceBear PW;
    //.........................................


    // --------- Player movements --------------
    //int playerPosX = 50;
    //int playerPosY = 50;
    //.........................................

    

    public GamePanel(int frameWidth, int frameHeight) {

        panelWidth = frameWidth;
        panelHeight = frameHeight;
        addKeyListener(this);
        addMouseListener(this);
        setFocusable(true); //fokuserar på detta "objektet" så man kan använda keylistener typ nåt sånt
        setFocusTraversalKeysEnabled(false); //tar bort focus för typ TAB, shift och sånt

        highscoreHandler = new HighScoreHandler(this, panelWidth, panelHeight);
        menu = new Menu(this, panelWidth, panelHeight);
        bg = new BackGround(panelWidth, panelHeight);
        mouseLocator = new MouseLocator();
        mPlayer = new Player();
        PW = new SpaceBear(0,0);
    }

    public void drawBearHealth(Graphics g) {
        g.setColor(Color.RED);
        g.setFont(new Font("serif", Font.BOLD, 50));
        g.drawString(Integer.toString(PW.getHealth()), 70, 600);
    }

    public void setMenuIsOn(boolean menuIsOn ) {
        this.menuIsOn = menuIsOn;
    }

    public void setgameIsOn(boolean gameIsOn ) {
        this.gameIsOn = gameIsOn;
    }

    public void setShowHighScore( boolean showHighScore) {
        this.showHighScore = showHighScore;
    }

    public void paintComponent ( Graphics g ) {
        checkMousePos();
        if(menuIsOn) {
            menu.drawMenu(g, mouseLocator);
        }
        else if (gameIsOn) {
            bg.drawBackGround(g);
            mPlayer.drawPlayer(g);
            PW.drawEnemyBear(g);
            drawBearHealth(g);
            eventHandler(g);
            
        }
        else if(showHighScore) {
            //System.out.println("hej");
            highscoreHandler.drawHighScoreTable(g, mouseLocator);
        }
        repaint();
    }     

    public void eventHandler(Graphics g){
        mPlayer.moveShip();
        PW.bearMovement();
        mPlayer.startShooting();
        checkCollision();
        endGame(g);
    }

    public void checkCollision(){
        //Check if the player touches the enemy
        if(mPlayer.getXPos() >= PW.getXPosition() && mPlayer.getXPos() <= PW.getXPosition()+PW.getWidth() && mPlayer.getYPos() >= PW.getYPosition() && mPlayer.getYPos() <= PW.getYPosition()+PW.getHeight() && PW.getHealth() != 0){
            mPlayer.setHealth(0);
        }
        //Check if the player hits an enemy with the projectile
        if(mPlayer.getProjectileX() >= PW.getXPosition() && mPlayer.getProjectileX() <= PW.getXPosition()+PW.getWidth() && mPlayer.getProjectileY() >= PW.getYPosition() && mPlayer.getProjectileY() <= PW.getYPosition()+PW.getHeight() && PW.getHealth() > 0){
            mPlayer.setProjectileX(-100);
            mPlayer.setProjectileY(-100);
            mPlayer.setNewShot(false);
            mPlayer.addScore(10);
            PW.changeHealth(1);
        }
    }

    public void endGame(Graphics g){
        if(mPlayer.getHealth() <= 0){
            if(showCount<= 2000){
                g.setColor(Color.WHITE);
                g.setFont( new Font("serif", Font.BOLD, 30) );
                g.drawString("-=!YOU DIED, SUCKER!=-", panelWidth/4, panelHeight/2);
                showCount++;
            }
            else {
                //highscoreHandler.newHighScoreSetter(score);
                showCount = 0;
                gameIsOn = false;
                menuIsOn = true;
                mPlayer.setHealth(10);//resets health for next game
                mPlayer.resetPositions();
                PW.setHealth(100);//resets health for next game
            }
        }
        if(PW.getHealth() <= 0){
            if(showCount<= 2000){
                mPlayer.setProjectileX(-100);
                mPlayer.setProjectileY(-100);
                g.setColor(Color.WHITE);
                g.setFont( new Font("serif", Font.BOLD, 30) );
                g.drawString("-=!YOU BEAT THE GAME!=-", panelWidth/4, panelHeight/2);
                showCount++;
            }
            else {
                if(writeName){
                    writeName = false;
                    mPlayer.addScore(100); //give 100 points if game is completed!
                    System.out.println("playerscore: "+mPlayer.getScore());
                    highscoreHandler.newHighScoreSetter(mPlayer.getScore());
                    showCount = 0;
                    mPlayer.setHealth(10);//resets health for next game
                    mPlayer.resetPositions();
                    PW.setHealth(100);//resets health for next game
                    gameIsOn = false;
                    showHighScore = true; //show the highscores!
                    writeName = true;
                }
            }
        }
    }

    /**
     * Instantly checks for the mousePosition, since
     * its placed inside the paintComponent-method.
     */
    private void checkMousePos() {
        mouseX = MouseInfo.getPointerInfo().getLocation().x;
        mouseY = MouseInfo.getPointerInfo().getLocation().y;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Quit")) { //om knappen Quit trycks ned
            System.exit(1);
        }
        if (e.getActionCommand().equals("Start")) { // om knappen Start trycks ned.
            System.out.println("Start");
        }
    }
    public void mousePressed(MouseEvent e) {
        mousePressed = true;
        mouseLocator.setMousePositions( e.getX(), e.getY() );
    }
    public void mouseReleased(MouseEvent e) {
        mousePressed = false;
        mouseLocator.setMousePositions(-5, -5); //just so that
    }
    public void mouseClicked(MouseEvent e) {
        
    }
    public void mouseEntered(MouseEvent e) { }
    public void mouseExited(MouseEvent e) { }
    public void keyTyped(KeyEvent e) {  }
    public void keyReleased(KeyEvent e) {
        mPlayer.stopShip(e.getKeyCode());
        mPlayer.stopShooting(e.getKeyCode());
    }
    public void keyPressed(KeyEvent e) {
        mPlayer.checkButtons(e.getKeyCode());
    }


}
